﻿Imports System.Collections.Specialized
Imports System.IO
Imports System.Globalization
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports System.Collections.ObjectModel
Imports System
Imports System.ComponentModel
Imports System.Xml.Serialization
Imports System.Threading

Namespace Controlers

    Public Class LinksControler(Of TView As {UIElement, ILinksView(Of TView), IHeatView(Of TView), ITeamSystemView(Of TView)})
        Inherits ControlerBase(Of TView)


        Public Sub New(ByVal View As TView)
            MyBase.New(View)
            m_Items = New QuantumBitDesigns.Core.ObservableList(Of LinkItem(Of TView))(View.Dispatcher)
            Reset()
        End Sub

        Public Overrides Sub Reset()
            If Not Working Then
                UpdateItems_Async()
            End If
        End Sub

        Private m_Items As QuantumBitDesigns.Core.ObservableList(Of LinkItem(Of TView))
        Private m_HasItems As Boolean = False
        Private m_WaitingForItems As Boolean = False
        Private m_SelectedItem As LinkItem(Of TView)

        Public Property SelectedItem() As LinkItem(Of TView)
            Get
                Return m_SelectedItem
            End Get
            Set(ByVal value As LinkItem(Of TView))
                If Not value Is m_SelectedItem Then
                    m_SelectedItem = value
                    OnPropertyChanged("SelectedItem")
                End If
            End Set
        End Property

        Public ReadOnly Property Items() As ObservableCollection(Of LinkItem(Of TView))
            Get
                If m_Items Is Nothing Then
                    Return Nothing
                End If
                Return m_Items.ObservableCollection
            End Get
        End Property

        Public Property HasItems() As Boolean
            Get
                Return m_HasItems
            End Get
            Private Set(ByVal value As Boolean)
                If Not m_HasItems = value Then
                    m_HasItems = value
                    OnPropertyChanged("HasItems")
                End If
            End Set
        End Property


        Private Sub UpdateItems()
            Working = True
            While (View.HeatControler.IsConnected And View.TeamSystemControler.HasSelectedServer And View.HeatControler.HasSelectedApplication) = False
                ' Wait
                Thread.Sleep(1000)
            End While
            ' Do the work
            HasItems = False
            SelectedItem = Nothing
            m_Items.Clear()
            ' Load Items
            For Each i In View.HeatControler.PendingApplications
                m_Items.Add(New LinkItem(Of TView)(Me, i))
                Thread.Sleep(100)
            Next
            HasItems = True
            Working = False
        End Sub

        Public Sub UpdateItems_Async()
            Dim action As New Action(AddressOf UpdateItems)
            action.BeginInvoke(Nothing, Nothing)
        End Sub


    End Class

End Namespace